package com.wngbms.fileio.sharemem;

import java.nio.charset.StandardCharsets;

public class ShareMemRead {

    public static void main(String[] args) {
        ShareMemory shareMemory = new ShareMemory();

//        m1(shareMemory);
        byte[] buf = new byte[100];
        for (; ; ) {
            shareMemory.read(0, buf.length, buf);
            System.out.print(new String(buf, StandardCharsets.UTF_8));
            sleep(500);
        }

    }

    private static void m1(ShareMemory shareMemory) {
        String s = "小米手机就是好用 Windows 11 Sony Apple " + 0 + "\r\n";
        int length = s.getBytes(StandardCharsets.UTF_8).length;

        new Thread(() -> {

            byte[] buf = new byte[length];
            shareMemory.read(0, length, buf);
            System.out.println(new String(buf, StandardCharsets.UTF_8));

        }).start();
    }

    public static void sleep(long t) {
        try {
            Thread.sleep(t);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
